<?php

    require ( 'init.inc.php' );
    require ( 'checkadmin.inc.php');
    require ( __COMM_PATH . 'pagecontrol.inc.php' );
    require ( __COMM_PATH . 'checkform.func.php' );
    require ( __CLASS_PATH . 'page_edit.class.php' );

    // -------------------------------------------------------
    // 接收传递过来的变量
    // -------------------------------------------------------
    $action = $_REQUEST["action"];
    $id     = addslashes($_REQUEST["id"]);
	   
    // ----------------- --------------------------------------
    // 页面基本设置
    // -------------------------------------------------------
    $page_title   = $ptitle["news_add"];                        // 页面标题
    $page_type    = "add";                                      // 页面类型
    $display_page = "planning_edit.htm";                            // Smarty 模板页面
    $table_name   = "{$tablepre}planning";                          // 表名
    $back_page    = "order_list.php?sortby={$_GET['sortby']}&oexp={$_GET['oexp']}&page={$_GET['page']}&classid={$classid}&keyword={$_GET['keyword']}";

      // ----------------- --------------------------------------
    // 读出订单的数据
    // -------------------------------------------------------
	   if($id){
			$result = $db->query("select `username`,`ordernum`,`useremail` from `website_order` where `id`='$id'");
			$row = $db->fetch_array($result);
		}
		
    if ($action=="submit") {
		
	     // ----------------- --------------------------------------
        // 接收数据并发送邮件
       // -------------------------------------------------------
        $title = dhtmlspecialchars($_POST["planning_title"]);
        $Recipient = $_POST["Recipient"];
        $type = intval($_POST["email_type"]);
		$email=$_POST['useremail'];
		$price = intval($_POST["planningprice"]);
        $content = $_POST["content"];
		$ordernum_planning = filter_str($_POST['ordernum']);
		//把定单号写入session;
		session_start();
		$_SESSION['ordernum_planning'] = $ordernum_planning;
$zhufulink=<<<zhufu
<p style="color:red;"><a href="http://localhost/paytype_planning.php">支付活动费用</a></p>
zhufu;
		$newcontent = $content.$zhufulink;
        $addtime = time();
        $adduser = dhtmlspecialchars($_POST["adduser"]);
			// -------------------------------------------------------
			// 发送邮件
			// -------------------------------------------------------
			$emailSubject = "策划方案";
            $sendPort = $site_mail_port;
            $sendHost = $site_mail_smtp;
            $emailName = $site_mail_name;
            $emailPwd = $site_mail_pwd;
            $emailSendFrom = $sitemail;
            $toEmail = empty($_POST['useremail'])?$Recipient:$_POST['useremail'];
            $setssl = $site_mail_ssl==0?false:true;
			sendEmail($emailSubject,$newcontent,$sendPort,$sendHost,$emailName,$emailPwd,$emailSendFrom,$toEmail,$setssl);
			
		if($type == "1"){
             $fields_arr = array(
             "title" => $title,
			 "planningprice" => $price,
             "ordernum" => filter_str($_POST['ordernum']),
             "content" => $newcontent,
             "addtime" => $addtime,
             "adduser" => $adduser
       		);
			 $db->query("update `website_order` set `trueprice`='$price'");
		    file_log($_SESSION['AdminUser'],"添加了策划方案");
		}
    }
    //print_r($fields_arr);
	
    // -------------------------------------------------------
    // 解析基本信息
    // -------------------------------------------------------
    $tpl->assign('addtime', date("Y-m-d H:i:s"));
    $tpl->assign('adduser', $_SESSION["AdminUser"]);
    $tpl->assign('useremail', $row['useremail']);
	$tpl->assign("ordernum", $row['ordernum']);
    // -------------------------------------------------------
    // 执行页面处理
    // -------------------------------------------------------
    show_page_head($page_type, $page_title);
    $pedit = new PageEdit($page_title, $back_page, $page_type); // 实例化一个编辑页面
    $pedit->SetParam($table_name, $condition);                  // 设置数据库基本参数，查询条件
    $pedit->InsertData($fields_arr);                            // 更新数据
    $pedit->TplParse($display_page);                            // 自动解析相关数据到 Smarty 模板
    show_page_foot();
    $db->close();
?>